package com.dongnam.quanly.common.model;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerators;

import com.dongnam.quanly.common.ActionCategory;
import com.dongnam.quanly.common.ActionType;
import com.dongnam.quanly.common.ObjectType;
import com.dongnam.quanly.common.ScreenId;

@Entity
@Table(name = "audittraillog")
public class AuditTrailLog implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ID")
	private Long id;
	@Column(name = "ACTIONTIME")
	private Timestamp actionTime;
	@Column(length = 40, nullable = false, name = "ACTIONEENAME")
	private String actioneeName;
	@Column(length = 30, nullable = false, name = "ACTIONEEIP")
	private String actioneeIp;
	@Column(length = 40, nullable = false, name = "SCREENID")
	@Enumerated(EnumType.STRING)
	private ScreenId screenId;
	@Column(length = 40, nullable = false, name = "ACTIONCATEGORY")
	@Enumerated(EnumType.STRING)
	private ActionCategory actionCategory;
	@Column(length = 40, nullable = false, name = "ACTIONTYPE")
	@Enumerated(EnumType.STRING)
	private ActionType actionType;
	@Column(length = 16, nullable = false, name = "RESULT")
	private String result;
	@Column(length = 50, nullable = false, name = "OBJECT")
	@Enumerated(EnumType.STRING)
	private ObjectType object;
	@Column(length = 100, name = "OBJECTID")
	private String objectId;
	@Column(length = 300, name = "ORIGINVAL")
	private String originVal;
	@Column(length = 300, name = "NEWVAL")
	private String newVal;
	@Column(length = 100, name = "CAUSE")
	private String cause;

	// @Column(length = 200)
	// private String details;

	public AuditTrailLog() {
	}

	public AuditTrailLog(Timestamp actionTime, String actioneeName, String actioneeIp, ScreenId screenId,
			ActionCategory actionCategory, ActionType actionType, String result, ObjectType object, String objectId,
			String originVal, String newVal, String cause /* String details */) {
		this.actionTime = actionTime;
		this.actioneeName = actioneeName;
		this.actioneeIp = actioneeIp;
		this.screenId = screenId;
		this.actionCategory = actionCategory;
		this.actionType = actionType;
		// this.details = details;
		this.result = result;
		this.object = object;
		this.objectId = objectId;
		this.originVal = originVal;
		this.newVal = newVal;
		this.cause = cause;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Timestamp getActionTime() {
		return actionTime;
	}

	public void setActionTime(Timestamp actionTime) {
		this.actionTime = actionTime;
	}

	public String getActioneeName() {
		return actioneeName;
	}

	public void setActioneeName(String actioneeName) {
		this.actioneeName = actioneeName;
	}

	public String getActioneeIp() {
		return actioneeIp;
	}

	public void setActioneeIp(String actioneeIp) {
		this.actioneeIp = actioneeIp;
	}

	public ScreenId getScreenId() {
		return screenId;
	}

	public void setScreenId(ScreenId screenId) {
		this.screenId = screenId;
	}

	public ActionCategory getActionCategory() {
		return actionCategory;
	}

	public void setActionCategory(ActionCategory actionCategory) {
		this.actionCategory = actionCategory;
	}

	public ActionType getActionType() {
		return actionType;
	}

	public void setActionType(ActionType actionType) {
		this.actionType = actionType;
	}

	// public String getDetails() {
	// return details;
	// }
	//
	// public void setDetails(String details) {
	// this.details = details;
	// }

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public ObjectType getObject() {
		return object;
	}

	public void setObject(ObjectType object) {
		this.object = object;
	}

	public String getObjectId() {
		return objectId;
	}

	public void setObjectId(String objectId) {
		this.objectId = objectId;
	}

	public String getOriginVal() {
		return originVal;
	}

	public void setOriginVal(String originVal) {
		this.originVal = originVal;
	}

	public String getNewVal() {
		return newVal;
	}

	public void setNewVal(String newVal) {
		this.newVal = newVal;
	}

	public String getCause() {
		return cause;
	}

	public void setCause(String cause) {
		this.cause = cause;
	}

	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("actionTime: " + actionTime + ";");
		buffer.append("actioneeName: " + actioneeName + ";");
		buffer.append("actioneeIp: " + actioneeIp + ";");
		buffer.append("screenId: " + screenId + ";");
		buffer.append("actionCategory: " + actionCategory + ";");
		buffer.append("actionType: " + actionType + ";");
		buffer.append("result: " + result + ";");
		buffer.append("object: " + object + ";");
		buffer.append("objectId: " + objectId + ";");
		buffer.append("originVal: " + originVal + ";");
		buffer.append("newVal: " + newVal + ";");
		buffer.append("cause: " + cause + ";");
		// buffer.append("details: "+details+";");
		return buffer.toString();
	}

	public boolean isFurtherDetails() {
		boolean further = true;
		if (cause == null || cause.equals(""))
			if (originVal == null || originVal.equals(""))
				if (newVal == null || newVal.equals(""))
					if (!actionType.equals(ActionType.UPDATE))
						further = false;
		return further;
	}

	public boolean isLastViewShow() {
		if (originVal == "" && newVal == "" && actionType.equals(ActionType.UPDATE) && result.equals("SUCCESS"))
			return true;
		else
			return false;
	}
}